package iedcyt.acute.view.widgets;

import iedcyt.acute.dataAccess.domain.acute1.CollGroupData;

import java.text.Collator;
import java.util.Locale;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class CollGroupDataTable extends NormalizationTable<CollGroupData> {
	
	public CollGroupDataTable(Composite composite, int style)
	{
		super(composite, style);
		LABEL = "Propiedades del grupo";
		_label.setText(LABEL);
		
		_table.setHeaderVisible(true);
		String[] titles = { "IdGr", "Propiedad", "Valor" };
		String[] toolTips = new String[] { "Identificador del grupo",
				"Nombre de la propiedad", "Valor de la propiedad" };

		for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
			TableColumn column = new TableColumn(_table, SWT.NULL);
			column.setText(titles[loopIndex]);
			column.setToolTipText(toolTips[loopIndex]);
		
		}

		addSort();
	}
	

	
	private void addSort()
	{
		_table.getColumn(0).addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				// sort column 1
				TableItem[] items = _table.getItems();
				for (int i = 1; i < items.length; i++) {
					int value1 = Integer.parseInt(items[i].getText(0));
					for (int j = 0; j < i; j++) {
						int value2 = Integer.parseInt(items[j]
								.getText(0));
						if (value1 < value2) {
							TableItem item = new TableItem(_table, SWT.NONE, j);
							item.setText(getItemValues(items[i]));
							items[i].dispose();
							items = _table.getItems();
							break;
						}
					}
				}
			}
		});
		
		_table.getColumn(1).addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				// sort column 2
				TableItem[] items = _table.getItems();
				Collator collator = Collator.getInstance(Locale.getDefault());
				for (int i = 1; i < items.length; i++) {
					String value1 = items[i].getText(1);
					for (int j = 0; j < i; j++) {
						String value2 = items[j].getText(1);
						if (collator.compare(value1, value2) < 0) {
							TableItem item = new TableItem(_table, SWT.NONE, j);
							item.setText(getItemValues(items[i]));
							items[i].dispose();
							items = _table.getItems();
							break;
						}
					}
				}
			}
		});
	}

	
	@Override
	protected void setItemElement(CollGroupData element, TableItem item) {
		item.setText(0, element.get_idGr() + "");
		item.setText(1, element.get_dataName());
		item.setText(2, element.get_dataValue());
		
	}



	
}
